/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.Faqs;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class FAQsDAO {
    private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<Faqs> GetAll() {
        ArrayList<Faqs> listFaq = new ArrayList<Faqs>();
        Faqs faq = null;
        try {

            strCommand = "select * from tbl_Faqs";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
                faq = new Faqs();

               faq.setFaqID(rs.getInt("faqID"));
               faq.setQuestion(rs.getString("question"));
               faq.setAnswer(rs.getString("answer"));
               faq.setFaqStatus(rs.getInt("faqStatus"));

                listFaq.add(faq);
            }
            return listFaq;
        } catch (SQLException ex) {
            Logger.getLogger(FAQsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listFaq;
    }

    public boolean AddFAQs(String question, String answer) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_Faqs (question,answer,faqStatus) VALUES (?,?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setString(1, question);
            ps.setString(2, answer);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(FAQsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public Faqs GetByID(int faqID) {

        Faqs faq = new Faqs();
        try {

            strCommand = "select * from tbl_Faqs WHERE faqID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, faqID);
            rs = ps.executeQuery();
            while (rs.next()) {
               faq.setFaqID(rs.getInt("faqID"));
               faq.setQuestion(rs.getString("question"));
               faq.setAnswer(rs.getString("answer"));
               faq.setFaqStatus(rs.getInt("faqStatus"));
            }
            return faq;
        } catch (SQLException ex) {
            Logger.getLogger(FAQsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return faq;
    }

    public boolean changeStatus(int faqID, int faqStatus) {
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_Faqs SET faqStatus = ? WHERE faqID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, faqStatus);
            ps.setInt(2, faqID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean editFAQs(Faqs objFAQs) {
        //UPDATE tbl_Banks SET bankName = ? WHERE bankID = ?
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_Faqs SET question = ? ,answer = ? WHERE faqID = ? ";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, objFAQs.getQuestion());
            ps.setString(2, objFAQs.getAnswer());
            ps.setInt(3, objFAQs.getFaqID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteFAQs(int faqID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_Faqs WHERE faqID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, faqID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
